package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 *覆盖
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.print("请输入图书的名字：");
        int currentSize = bookList.getUesSize();
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int index = -1;//存下标
        int i = 0;
        for ( ; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("找到了！");
                index = i;
                System.out.println(book);
                System.out.println();
            }
        }
        if (i >= currentSize) {
            System.out.println("找不到此书");
            return;
        }
        //直接覆盖
        for (int j = index; j < currentSize-1; i++) {
            bookList.setBook(j,bookList.getBook(j +1));
        }
        bookList.setUesSize(bookList.getUesSize()-1);   //更新数量
        bookList.setBook(currentSize-1,null);  //置为空   相当于free
        System.out.println("删除成功！");
        System.out.println();
    }
}
